﻿using Core;
using DataAccess;
using KhoaLuan.MainSys.Controls;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Configuration;
using System.Configuration;

namespace KhoaLuan.MainSys
{
    public partial class MasterPage : System.Web.UI.MasterPage
    {
        public string CurrentUser { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                getMenu();
            xulyClickEven_Menu();
        }
        private List<MenuWeb> getMenu()
        {
            var _orgid = Request.QueryString["OrgID"];
            int orgid = 1;
            if (_orgid != null)
                orgid = int.Parse(_orgid);
            MenuWeb_Proxy menuProxy = new MenuWeb_Proxy();
            DataTable dt = menuProxy.SelectByOrg(orgid);
            List<MenuWeb> mnutam = new List<MenuWeb>();
            DataRow[] drowpar = dt.Select("ParentID=" + 0);
            foreach (DataRow dr in drowpar)
            {
                menuBar.Items.Add(new MenuItem(dr["Name"].ToString(),
                        dr["id"].ToString(), ""));
                mnutam.Add(new MenuWeb
                {
                    id = ConvertType.ToInt(dr["id"].ToString()),
                    Name = dr["Name"].ToString(),
                    ParentID = ConvertType.ToInt(dr["ParentID"].ToString()),
                    Link = dr["Link"].ToString(),
                    OrgID = ConvertType.ToInt(dr["OrgID"].ToString())
                });
            }
            foreach (DataRow dr in dt.Select("ParentID >" + 0))
            {
                MenuItem mnu = new MenuItem(dr["Name"].ToString(), dr["id"].ToString(), "");
                int tam = menuBar.Items.Count;
                if (menuBar.FindItem(dr["ParentID"].ToString()) != null)
                {
                    menuBar.FindItem(dr["ParentID"].ToString()).ChildItems.Add(mnu);
                }
                mnutam.Add(new MenuWeb
                {
                    id = ConvertType.ToInt(dr["id"].ToString()),
                    Name = dr["Name"].ToString(),
                    ParentID = ConvertType.ToInt(dr["ParentID"].ToString()),
                    Link = dr["Link"].ToString(),
                    OrgID = ConvertType.ToInt(dr["OrgID"].ToString())
                });
            }
            
            return mnutam;
        }
        public void xulyClickEven_Menu()
        {
            menuBar.MenuItemClick += mnu_MenuItemClick;
        }
        private void mnu_MenuItemClick(object sender, MenuEventArgs e)
        {
            try
            {
                Menu menu = ((Menu)(sender));
                int menuId = int.Parse(menu.SelectedItem.Value);
                var proxy = new MenuWeb_Proxy();
                var _menu = proxy.GetById_MenuWeb_Proxy(menuId).ToObject<MenuWeb>();
                if (_menu != null && _menu.Link != null && !string.IsNullOrWhiteSpace(_menu.Link))
                {
                    if (_menu.Link.Trim().ToLower().Contains("orgid"))
                        Response.Redirect(_menu.Link.Trim());
                    else
                    {
                        if (_menu.Name.ToLower() == "trang chủ")
                        {
                            if (_menu.Link != null)
                                Response.Redirect(_menu.Link.Trim());
                        }
                        string link = _menu.Link.Trim();
                        if (!link.Contains("?"))
                            link += "?";
                        var orgId = Request.QueryString["OrgID"];
                        if (orgId != null)
                            Response.Redirect(link + "&OrgID=" + orgId);
                        else
                            Response.Redirect(link + "&OrgID=" + 1);
                    }
                }
            }
            catch (Exception)
            {
            }
        }
        protected void btnLogout_Click(object sender, EventArgs e)
        {
            FormsAuthentication.SignOut();
            Response.Redirect("~/Account/Login.aspx?returnUrl=" + Request.Url.PathAndQuery);
        }

    }
}